<?php
/*
 *   用于MySQL的数据库存取处理类
 *
 *   Author : 周环
 *   Date   : 2008-09-08
 *
 */ 

class MysqlDB {
  
  /* public: connection parameters */
  var $host     = "localhost";
  var $database = "juxiezuo";
  var $user     = "root";
  var $password = "";

  /* public: configuration parameters */
  var $isAutoFree      = 0;   
  var $isHaltOnError     = "yes";
  var $isPersistentConnect  = 0;   

  /* public: result array and current row number */
  var $record   = array();
  var $row = 0;

  /* private: link and query handles */
  var $linkHandler  = 0;
  var $queryHandler  = 0;


  /* public: connection management */
  function connect() {

    $h = $this->host;
	$d = $this->database;
    $u = $this->user;
	$p = $this->password;
      
    /* establish connection, select database */
    if ( 0 == $this->linkHandler ) {

	  if(!$this->isPersistentConnect) {
		$this->linkHandler = @mysql_connect($h, $u, $p);
	  } else {
		$this->linkHandler = @mysql_pconnect($h, $u, $p); 
	  }

	  if (!$this->linkHandler) {
		$this->showError("connect server failed.");
		return 0;
	  }

	  if (!@mysql_select_db($d, $this->linkHandler)) {
		$this->showError("cannot use database ".$d);
		return 0;
	  }
    }
    
    return $this->linkHandler;
  }

  /* public: discard the query result */
  function free() {
      @mysql_free_result($this->queryHandler);
      $this->queryHandler = 0;
  }

  var $query_charset = "utf8";
  function set_query_charset($v) {
	 $this->query_charset = $v;
  }

  /* public: perform a query */
  function query($sql="") {

    if ($sql == "") {
      return 0;
	}
    if (!$this->connect()) {
      return 0;
    }
    if ($this->queryHandler) {
      $this->free();
    }

	mysql_query("SET NAMES '".$this->query_charset."'", $this->linkHandler); // use the utf-8; @2012-07-03
    $this->queryHandler = @mysql_query($sql,$this->linkHandler);
    $this->row   = 0;

    if (!$this->queryHandler) {
      $this->showError("invalid sql : ". $sql);
    }

    return $this->queryHandler;
  }

  /* public: walk result set */
  function nextRecord() {
    if (!$this->queryHandler) {
      $this->showError("please do query before the nextRecord().");
      return 0;
    }

    $this->record = @mysql_fetch_array($this->queryHandler);
    $this->row   += 1;

    $status = is_array($this->record);
    if (!$status && $this->isAutoFree) {
      $this->free();
    }
    return $status;
  }

  /* public: position in result set */
  function seek($pos = 0) {
    if (@mysql_data_seek($this->queryHandler, $pos)) { 
      $this->row = $pos;
	  return 1;
	} else {
      $this->showError("seek($pos) failed: result has ".$this->getRowCount()." rows.");
      return 0;
    }
  }

  /* public: evaluate the result (size, width) */
  function getRowCount() {
    return @mysql_num_rows($this->queryHandler);
  }

  function getColumnCount() {
    return @mysql_num_fields($this->queryHandler);
  }

  /* public: get the current record */
  function getCurrentRecord() {
      return $this->record;
  }

  /* public: get the column value */
  function getColumnValue($column) {
	$column = trim($column);
	$record = &$this->record;
    if ( isset($record[$column]) ) {
      return $record[$column];
    }
  }

  /* public: some aliasname for legacy code */
  function f($column) {
    return $this->getColumnValue($column);
  }

  function next_record() {
    return $this->nextRecord();
  }

  function num_rows() {
    return $this->getRowCount();
  }


  /* private: error handling */
  function showError($runtimeMsg) {
    $errorMsg = @mysql_error($this->linkHandler);
    $errorNo  = @mysql_errno($this->linkHandler);

	printf("<div style='border:gray 1px solid; padding:3px; 
			background:#f4f4f4; margin:3px; color:blue'>
			<b>runtime error</b> -> %s <br>
			<b>database error</b> -> %s (%s)<br></div>", $runtimeMsg, $errorMsg, $errorNo);

    if ($this->isHaltOnError == "yes") {
      die("execution is terminated.");
	}

  }

}


/*
// Sample Code

$db = new DB();
$db->query("sql");

while($db->nextRecord()) {
	echo $db->getColumnValue("field");
}
*/

?>
